package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Adminoldpassword;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

/**
 * 个人中心
 *
 * @author zhoubin
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin")
public class AdminInfoController {

    @Autowired
    private IAdminService adminService;

    //上传头像并更新当前用户的头像（使用七牛云）
    @RequestMapping(value="/userface", method= RequestMethod.POST)
    public RespBean uploadImg(@RequestParam MultipartFile file, HttpServletRequest request) {

        System.out.println(file);
        RespBean result = new RespBean();
        if (file.isEmpty()) {
            result.error(400,"文件为空，请重新上传");
            return result;
        }

        try {
            byte[] bytes = file.getBytes();
            String imageName = UUID.randomUUID().toString();


            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                result.success("文件上传成功",url);
                System.out.println(url);
                adminService.updatetu(url,request);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {

            result.error("文件上传发生异常！");
            return result;
        }

    }

    //更新用户数据
    @PutMapping("/info")
    public RespBean updeteAdmin(@RequestBody Admin adminvo){

        return adminService.updateAdminService(adminvo);
    }


    //修改密码
    @PutMapping("/pass")
    public RespBean updeteAdminpass(@RequestBody Adminoldpassword adminpws){
        System.out.println(adminpws.getAdminId());
        System.out.println(adminpws);
        return adminService.updateAdminpassService(adminpws);
    }

}